package com.devunion.bogdan.view;

import com.devunion.bogdan.model.project.*;
import com.devunion.bogdan.services.SearchService;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.*;
import java.util.List;
import java.util.Map;
import java.util.HashMap;

/**
 * @author Viktar Liaskovich (devunion@gmail.com)
 */
public class SearchProjectsPanel extends JPanel {
    private ResultsPanel resultsPanel;
    private PaperOpeningListener paperOpeningListener;

    public SearchProjectsPanel() {
        super(new BorderLayout());

        initComponents();
    }

    private void initComponents() {
        add(new SearchPanel(), BorderLayout.NORTH);

        resultsPanel = new ResultsPanel();
        add(resultsPanel, BorderLayout.CENTER);
    }

    private class SearchPanel extends JPanel {
        private JTextField jTextFieldPaperName;
        private JComboBox jComboBoxPaperType;

        private SearchPanel() {
            super(new FlowLayout(FlowLayout.LEFT));

            initComponents();
        }

        private void initComponents() {
            jTextFieldPaperName = new JTextField();
            jTextFieldPaperName.setColumns(20);
            jComboBoxPaperType = new JComboBox(new String[]{"ANY", PaperType.ARTICLE.toString(), PaperType.BOOK.toString(), PaperType.DOCUMENT.toString(), PaperType.LETTER.toString(), PaperType.OTHER.toString()});

            add(new JLabel("Paper name:"));
            add(jTextFieldPaperName);
            add(new JLabel("Paper type:"));
            add(jComboBoxPaperType);

            final JButton jButtonSearch = new JButton("Search");
            jButtonSearch.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    List<Paper> papers = SearchService.getInstance().search(jTextFieldPaperName.getText(), jComboBoxPaperType.getSelectedItem().toString());

                    resultsPanel.fillTable(papers);
                }
            });
            add(jButtonSearch);
        }
    }

    private class ResultsPanel extends JPanel {
        private JTable jTable;
        private Map<Integer, Paper> row2paper = new HashMap<Integer, Paper>();

        private ResultsPanel() {
            super(new BorderLayout());

            initComponents();
        }

        private void initComponents() {
            jTable = new JTable() {
                public boolean isCellEditable(int rowIndex, int vColIndex) { 
                    return false;
                }
            };

            jTable.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    if (e.getButton() == MouseEvent.BUTTON1 && e.getClickCount() == 2) {
                        int row = ((JTable) e.getSource()).getSelectedRow();
                        Paper paper = row2paper.get(row);

                        if (paperOpeningListener != null) {
                            paperOpeningListener.openPaper(paper);
                        }
                    }
                }
            });

            add(jTable);
        }

        public void fillTable(List<Paper> papers) {

            final DefaultTableModel model = new DefaultTableModel(new Object[][]{}, new String[]{"Project name", "Paper name", "Type", "Pages"});

            int row = 0;
            for (Paper paper : papers) {
                final PaperInfo info = paper.getInfo();

                Project project = ProjectManager.getInstance().getProjectForPaper(paper.getPaperId());
                model.addRow(new Object[]{project.getName(), info.getTitle(), info.getType(), paper.getPages().size()});

                row2paper.put(row, paper);
                row++;
            }

            jTable.setModel(model);
        }
    }

    public void setPaperOpeningListener(PaperOpeningListener paperOpeningListener) {
        this.paperOpeningListener = paperOpeningListener;
    }
}
